<template>
  <el-dialog v-model="dialogShow11" width="30%">
    <div class="titleShow">
      <div class="titles">{{ $t('licence.sys_fixed') }}</div>
    </div>
    <el-radio style="display: block; margin-bottom: 20px" v-model="switchG" label="1">{{
      $t('licence.sys_kaiqi') }}</el-radio>
    <el-radio style="display: block; margin-bottom: 20px" v-model="switchG" label="2">{{
      $t('licence.sys_guanbi') }}</el-radio>
    <el-form :inline="true" :model="formInline" :rules="rules" ref="formInlines" class="demo-form-inline"
      v-show="switchG == 1">
      <el-form-item :label="$t('licence.sys_zhilingfixed')" prop="num_6">
        <el-input style="width: 300px" v-model="formInline.num_6"
          :placeholder="$t('licence.sys_shuzhi0')"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogShow11 = false">{{ $t('licence.cancel') }}</el-button>
        <el-button type="primary" @click="instruction(11)">{{ $t('licence.determine') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { commandControl,getPsListByIds } from "@/api/inveter/inveter";
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const dialogShow11 = ref(false) //固定有功调节
const props = defineProps(['ids','lang','powerStation']);
const formInline = ref({
  num_6: '' as any,
})
const env = import.meta.env.VITE_APP_ENV
const formInlines = ref<ElFormInstance>();
const rules = reactive({
  num_6: [
    {
      required: true,
      message: t("licence.sys_shurushuzhi"),
      trigger: "blur",
    },
  ],
});
const switchG = ref<number | string>(1) //固定有功调节
const emits = defineEmits('compareResult');
const instruction = async ()=>{
  if (switchG.value == 1) {
      if (formInline.value.num_6 >= 0) {
        await formInlines.value?.validate();
        ElMessageBox.confirm(t("licence.Submit"), t("licence.Tips"), {
          confirmButtonText: t("licence.determine"),
          cancelButtonText: t("licence.cancel"),
          type: "warning",
        })
          .then(async () => {
            let data = {
              commandId: 11,
              ids: props.ids.join(","),
              setVal: formInline.value.num_6,
              type: switchG.value,
              lang: props.lang,
            };
            await commandControl(data);
            if(env=='staging') {
                ElMessage({
                  type: "success",
                  duration: 40000,
                  message: t("licence.sys_restoreFactory_compare"),
                });
                setTimeout(async () => {
                  let data = {
                    stationId: props.powerStation.value.id,
                    commandId: 12,
                    ids: -1,
                    lang: props.lang,
                  };
                  commandControl(data);
                }, 30000);
                setTimeout(async () => {
                  let res = await getPsListByIds(props.ids.join(","));
                  emits('compareResult',res.data,9);
                }, 40000);
            }
            else{
              ElMessage({
                type: "success",
                message: t("licence.success"),
              });
            }
          })
          .catch(() => {
            ElMessage({
              type: "info",
              message: t("licence.cancels"),
            });
          });
        dialogShow11.value = false;
      } else {
        ElMessage({
          type: "error",
          message: t("licence.sys_shuzhi0"),
        });
      }
    } else {
      ElMessageBox.confirm(t("licence.Submit"), t("licence.Tips"), {
        confirmButtonText: t("licence.determine"),
        cancelButtonText: t("licence.cancel"),
        type: "warning",
      })
        .then(async () => {
          let data = {
            commandId: 11,
            ids: props.ids.join(","),
            // setVal: formInline.value.num_6,
            type: switchG.value,
            lang: props.lang,
          };
          await commandControl(data);
          ElMessage({
            type: "success",
            message: t("licence.success"),
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: t("licence.cancels"),
          });
        });
      dialogShow11.value = false;
    }
}
const openDialog = async () => {
  dialogShow11.value = true;
  formInline.value.num_6 = "";
  switchG.value = "1"; //固定有功调节
}
defineExpose({
  openDialog
})
</script>
